﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.Utils;
using System.Collections.Generic;
using Ccit.Utils;
using Newtonsoft.Json;
using Ccit.OA.UserControls;

namespace Ccit.Mgoa.Collaboration
{
    public partial class collaboration_pesonal_index : Ccit.Utils.Page
    {
        LoginUser loginUser = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            loginUser = (LoginUser)Session["LogUserInfo"];
            if (!IsPostBack)
            {
                
                hdSearchType.Value = "basic";
                InitDDL();
            }
            BindGrid();
        }

        private void BindGrid()
        {
            MsQuery msQuery = null;
            string sSearchType = hdSearchType.Value;
            if (sSearchType == "basic")  //基本查询
            {
                msQuery = new MsQuery();
                string sType = selCollaborate.Value;
                if (sType == "1") //协作标题
                {
                    msQuery.QueryText = " AND $$title$$ like ('%" + (txtCollaborativeTitle1.Value).Trim().Replace("'", "''") + "%')";
                }
                else if (sType == "2") //协作类型l
                {
                    string sCollaborativeType = selCollaborativeType1.Value;
                    if (sCollaborativeType != "")
                        msQuery.QueryText = " AND $$type$$='" + sCollaborativeType + "'";
                }
                else if (sType == "3") //发布人
                {
                    msQuery.QueryText = " AND $$publisher$$ like ('%" + (txtPublisher1.Value).Trim().Replace("'", "''") + "%')";
                }
                else if (sType == "4") //协作状态
                {
                    string sStatus = selCollaborativeStatus.Value;
                    if (sStatus != "")
                        msQuery.QueryText = " AND $$status$$ = '" + sStatus + "'";
                }
            }
            else if (sSearchType == "advanced") //高级查询
            {
                msQuery = new MsQuery();
                string sCollaborativeType = selCollaborativeType.Value;
                if (sCollaborativeType != "")                
                    msQuery.QueryText += " AND $$type$$='" + sCollaborativeType + "'";
                string sTitle = (txtCollaborativeTitle.Value).Trim();
                if (sTitle != "")
                    msQuery.QueryText += " AND $$title$$ like('%" + sTitle.Replace("'", "''") + "%')";
                string sStatus = selStatus.Value;
                if (sStatus != "")
                    msQuery.QueryText += " AND $$status$$='" + sStatus + "'";
                string sPublisher = (txtPublisher.Value).Trim();
                if(sPublisher!="")
                    msQuery.QueryText += " AND $$publisher$$ like ('%" + sPublisher.Replace("'", "''") + "%')";
                string sBeginDate = (txtBeginDate.Value).Trim();
                if (sBeginDate != "")
                    msQuery.QueryText += " AND $$publishDate$$>='" + sBeginDate + "'";
                string sEndDate = (txtEndDate.Value).Trim();
                if(sEndDate!="")
                    msQuery.QueryText += " AND $$publishDate$$<='" + sEndDate + "'";
            }
            CollaborateBLL oCollaborate = new CollaborateBLL();
            List<MsCollaborativeTopic> lstCollaborativeTopic = oCollaborate.GetCollaborativeTopicListByQuery(msQuery);
            gvCollaborationList.DataSource = lstCollaborativeTopic;
            gvCollaborationList.DataBind();
        }

        private void InitDDL()
        {
            CtrInterface.BindDropDown(ref selCollaborativeType, "CLB_A3", "CLB_A3_10", "CLB_A3_20", true, "全部", "");
            CtrInterface.BindDropDown(ref selCollaborativeType1, "CLB_A3", "CLB_A3_10", "CLB_A3_20", true, "全部", "");
        }

        protected void gvCollaborationList_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            loginUser = (LoginUser)Session["LogUserInfo"];
            if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;
            string sId = e.KeyValue.ToString();

            string sTitle = gvCollaborationList.GetRowValues(e.VisibleIndex, "Title").ToString();

            e.Row.Cells[1].Text = "<a href=\"javascript:doView('" + sId + "')\">" + sTitle + "</a>";


            string sTypeId = gvCollaborationList.GetRowValues(e.VisibleIndex, "TypeId").ToString();
            e.Row.Cells[2].Text = SysInterface.GetNameStrByIdStr("CLB_A3", "CLB_A3_10", "CLB_A3_20", sTypeId, "");
            string sPublisherId = gvCollaborationList.GetRowValues(e.VisibleIndex, "PublisherId").ToString();
            
            string sStauts = gvCollaborationList.GetRowValues(e.VisibleIndex, "Status").ToString();
            int iStatusCellIndex = gvCollaborationList.Columns["Status"].Index;
            if (sStauts == "0")
                e.Row.Cells[iStatusCellIndex].Text = "未发布";
            else if (sStauts == "1")
                e.Row.Cells[iStatusCellIndex].Text = "已生效";
            else if (sStauts == "2")
                e.Row.Cells[iStatusCellIndex].Text = "已失效";

            HtmlAnchor haEdit = (HtmlAnchor)gvCollaborationList.FindRowCellTemplateControl(e.VisibleIndex, null, "haEdit");
            HtmlAnchor haOperate = (HtmlAnchor)gvCollaborationList.FindRowCellTemplateControl(e.VisibleIndex, null, "haOperate");
            HtmlImage imgOperate = (HtmlImage)gvCollaborationList.FindRowCellTemplateControl(e.VisibleIndex, null, "imgOperate");
            DevExpress.Web.ASPxClasses.Internal.InternalWebControl chkCheck = (DevExpress.Web.ASPxClasses.Internal.InternalWebControl)e.Row.Cells[0].Controls[0];

            bool bManagePower=CheckHasPowerForType(sTypeId,"manage");
            if (sPublisherId == loginUser.UserId || bManagePower)
            {
                if (CheckHasPowerForType(sTypeId, "publish"))
                {
                    haEdit.Attributes["href"] = "javascript:doEdit('" + sId + "')";
                    haOperate.Attributes["href"] = "javascript:doOperate('" + sId + "','" + sStauts + "')";
                    if (sStauts == "0") //未发布
                    {
                        imgOperate.Src = "../images/toolbar/start.gif";
                        imgOperate.Alt = "启动协作";
                    }
                    else if (sStauts == "1") //已生效
                    {
                        imgOperate.Src = "../images/toolbar/stop.gif";
                        imgOperate.Alt = "终止协作";
                    }
                    else if (sStauts == "2") //已失效
                    {
                        imgOperate.Src = "../images/toolbar/restart.gif";
                        imgOperate.Alt = "重新发布";
                    }
                }
                else
                {
                    haEdit.Disabled = true;
                    haOperate.Disabled = true;
                    haOperate.InnerHtml = "&nbsp;&nbsp;&nbsp;&nbsp;";
                }
            }
            else
            {
                chkCheck.Enabled = false;
                haEdit.Disabled = true;
                haOperate.Disabled = false;
                haOperate.InnerHtml = "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
        }


        /// <summary>
        /// 判断当前用户是否有该协作类型的权限
        /// </summary>
        /// <param name="sTypeId">协作类型ID</param>
        /// <param name="sPowerType">权限类别 publish：发布权限 manage：管理权限</param>
        /// <returns></returns>
        private bool CheckHasPowerForType(string sTypeId, string sPowerType)
        {
            CollaborateBLL oCollaborate = new CollaborateBLL();
            bool bFlag=false;
            List<MsCollaborativeType> lstCollaborativeType=new List<MsCollaborativeType>();
            if (sPowerType.Equals("publish"))
            {
                lstCollaborativeType = oCollaborate.GetPowerRangeCollaborativeType("2");
            }
            else if (sPowerType.Equals("manage"))
            {
                lstCollaborativeType = oCollaborate.GetPowerRangeCollaborativeType("1");
            }

            if (lstCollaborativeType.Count > 0)
            {
                for (int i = 0; i < lstCollaborativeType.Count; i++)
                {
                    MsCollaborativeType message = lstCollaborativeType[i];
                    if (message.TypeId == sTypeId)
                    {
                        bFlag = true;
                        break;
                    }
                }
            }
            return bFlag;
        }

        protected void lnkSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
            DevExpress.Web.Data.WebDataSelection oSelection = gvCollaborationList.Selection;
            oSelection.UnselectAll();
        }

        [global::System.Web.Services.WebMethod]
        public static string OperateCollaborativeTopic(string sIds,string sFinishDate,bool bKeepCommInfo, string sOperateType)
        {
            string sResult = "";
            CollaborateBLL oCollaborate = new CollaborateBLL();
            bool bFlag = false;
            if (sOperateType == "delete")
            {                
                bFlag = oCollaborate.DeleteCollaboration(sIds);
            }
            else
            {
                bFlag = oCollaborate.ChangeCollaborativeStatus(sIds, sFinishDate, bKeepCommInfo, sOperateType);
            }
            var oResult = new { Operate = sOperateType, Result = bFlag };
            sResult = JavaScriptConvert.SerializeObject(oResult);
            return sResult;
        }


        [global::System.Web.Services.WebMethod]
        public static string GetCollaborativeTopic(string sId)
        {
            string sResult = "";
            if (sId != "")
            {
                CollaborateBLL oCollaborate = new CollaborateBLL();
                MsCollaborativeTopic message = oCollaborate.GetCollaborativeTopicById(sId);
                sResult = JavaScriptConvert.SerializeObject(message);
            }
            return sResult;
        }
    }
}
